<?php

/**
 * Admin_Modul File
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
 * @category Admin_Modul
 * @package  Admin_Modul_Package
 * @author Johari <joharijumali@gmail.com>  
 */

use Admin\Models\Modul\Flow as Flow;
use Admin\Models\Modul\Step as Step;
use Admin\Models\Modul\Page as Page;
use Admin\Models\Modul\Rule;
use Admin\Models\User\Role as Role;
use Laravel\Str as Str;
use Laravel\Log as Log;
use Laravel\URI as URI;
use Laravel\Input as Input;
use Admin\Libraries\Menu as Menu;
use Admin\Libraries\Crapcrush as Crapcrush;

/**
 * Admin_Modul class
 * 
 * Modul Management Class
 *
 * @category Admin_Modul_Controller
 * @package  Admin_Modul_Package
 * @author Johari <joharijumali@gmail.com>   
 */

class Admin_Modul_Controller extends Admin_Base_Controller
{

	public $restful = true;

	/**
     * Function get_index
	 *
	 * @return get_index.
     */
    public function get_index()
	{
    	return View::make('admin::modul.index');
    }
	
	/**
     * Modul Management function
     * Component : register
     * Method 	 : ajax post
	 *
	 * @return void.
     */
    public function get_register()
	{
    	return View::make('admin::modul.index');
    }

	/**
     * Function post_recomp
	 *
	 * @return post_recomp.
     */
    public function post_recomp()
	{
        Page::composer();

        Log::write('Module', 'Registering Modul & Page by <code>'.Auth::user()->username.'</code>');

        return Menu::page();
    }
	
	/**
     * Page Managment function
     * Component : page
     * Method 	 : ajax post
	 *
	 * @return void.
     */
    public function get_page()
	{
        $data['struct'] = Menu::page();

    	return View::make('admin::modul.page', $data);
    }
	
	/**
     * Function post_page
	 *
	 * @return post_page.
     */
    public function post_page()
	{
        $contents = Input::all();

        Page::setPages($contents);

        return Redirect::to('admin/modul/page');

        Log::write('Module', 'Modify Page Information by '.Auth::user()->username);
    }
	 
	/**
     * Flow Management function
     * Component : flow, flowinfo
     * Method 	 : ajax post
	 *
	 * @return void.
     */
    public function get_flow()
	{
        $data['flowlist'] = Flow::listFlow();
        return View::make('admin::modul.flow', $data);
    }
	
	/**
     * Function post_flow
	 *
	 * @return post_flow.
     */
    public function post_flow()
	{
        $input = Input::get();

        if ($input['sf_id'] == null) {
            $logs = 'Add New Flow ';
            $flow = new Flow;
        } else {
            $flow = Flow::find($input['sf_id']);
            $logs = 'Edit Flow ';
        }
        $flow->sf_name = Str::title($input['sf_name']);
        $flow->timestamp();
        $flow->save();

        Log::write('Modul', $logs.$input['sf_name'].' by '.Auth::user()->username);

        return Flow::listFlow();
    }

	/**
     * Function get_flowinfo
	 *
	 * @return get_flowinfo.
     */
    public function get_flowinfo()
	{
        $input = Input::get();

        $flow = Flow::find($input['sf_id'])->to_array();

        return json_encode($flow);
    }
	
	/**
     * Flow Step Management function
     * Component : step, stepinfo, resetstepdata,  deletestep
     * Method 	 : ajax post get
	 *
	 * @return void.
     */
    public function get_step()
	{
        $data['flow'] = Str::title(Flow::find(URI::segment(5))->sf_name);
        $data['steplist'] = Step::listSteps(URI::segment(5));
        $data['rulelist'] = Rule::listRule(URI::segment(5));
        $data['allrole'] = Role::arrayRoles();
        $data['steps'] = Step::arraySteps();
        $data['pagelist'] = $listpage = Page::listSteppages();

        return View::make('admin::modul.step', $data);
    }
	
	/**
     * Function post_step
	 *
	 * @return post_step.
     */
    public function post_step()
	{
        $input = Input::get();

        if ($input['sfs_id'] == null) {
            $logs = 'Add New Step ';
            $step = new Step;
        } else {
            $step = Step::find($input['sfs_id']);
            $logs = 'Edit Step ';
        }

        $step->sfs_step = Str::title($input['sfs_step']);
        $step->sf_id = $input['sf_id'];
        $step->timestamp();
        $step->save();

        Log::write('Modul', $logs.$input['sfs_step'].' by '.Auth::user()->username);

        return json_encode(Step::listSteps($input['sf_id']));
    }

    
    /**
     * Function post_step
     *
     * @return post_step.
     */
    public function get_rule()
    {
        $data['flow'] = Str::title(Flow::find(URI::segment(5))->sf_name);
        $data['steplist'] = Step::listSteps(URI::segment(5));
        $data['rulelist'] = Rule::listRule(URI::segment(5));
        $data['allrole'] = Role::arrayRoles();
        $data['steps'] = Step::arraySteps();
        $data['pagelist'] = $listpage = Page::listSteppages();

        return View::make('admin::modul.rule', $data);
    }

    /**
     * Function post_step
     *
     * @return post_step.
     */
    public function post_rule()
    {
        $input = Input::get();

        if ($input['sfr_id'] == null) {
            $logs = 'Add New Rule ';
            $step = new Rule;
        } else {
            $step = Rule::find($input['sfr_id']);
            $logs = 'Edit Rule ';
        }

        $step->sf_id = $input['sf_id'];
        $step->sfr_state = $input['sfr_state'];
        $step->sfs_id = $input['sfs_id'];
        $step->sfr_next = $input['sfr_next'];
        $step->sur_id = $input['sur_id'];
        $step->sfr_page = $input['sfr_page'];
        $step->timestamp();
        $step->save();

        Log::write('Modul', $logs.$input['sfr_id'].' by '.Auth::user()->username);

        return json_encode(Rule::listRule($input['sf_id']));
    }

    /**
     * Function get_stepinfo
     *
     * @return get_stepinfo.
     */
    public function get_stepinfo()
    {
        $input = Input::get();

        $step = Step::find($input['stepid'])->to_array();

        return json_encode($step);
    }

    /**
     * Function get_ruleinfo
     *
     * @return get_ruleinfo.
     */
    public function get_ruleinfo()
    {
        $input = Input::get();

        $rule = Rule::find($input['stepid'])->to_array();

        return json_encode($rule);
    }

    /**
     * Function post_deletestep
     *
     * @return post_deletestep.
     */
    public function post_deletestep()
    {
        $input = Input::get();

        Step::find($input['id'])->delete();

        Log::write('User', 'Delete Step '.$input['step'].' by '.Auth::user()->username);

        return json_encode(Step::listSteps($input['flowid']));
    }

    /**
     * Function post_deletestep
     *
     * @return post_deletestep.
     */
    public function post_deleterule()
    {
        $input = Input::get();

        Rule::find($input['id'])->delete();

        Log::write('User', 'Delete Rule '.$input['id'].' by '.Auth::user()->username);

        return json_encode(Rule::listRule($input['flowid']));
    }

	/**
     * Function get_resetnavdata
	 *
	 * @return get_resetnavdata.
     */
    public function get_resetnavdata()
	{

        $data['sfs_id'] = Step::arraySteps();
        $data['sfr_next'] = Step::arraySteps();

        return json_encode($data);
    }
	
	/**
     * Function get_resetstepdata
	 *
	 * @return get_resetstepdata.
     */
    public function get_resetstepdata()
	{
        $data['next'] = $data['condition1'] = $data['condition2'] = $list = Step::arraySteps();

        return json_encode($data);
    }


}

?>